package com.ciwong.epaper.modules.scan.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.Gallery;

/**
 * Created by res-zwyan on 2015/9/29.
 */
public class MyGallery extends Gallery
{
    private float gTouchStartX;

    private float gTouchStartY;

    private int scaledTouchSlop;

    private boolean isScroll = false;

    private MotionEvent e;

    public MyGallery(Context context)
    {
        super(context);
        init();
    }

    public MyGallery(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public MyGallery(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init()
    {
        scaledTouchSlop = ViewConfiguration.get(getContext())
                .getScaledTouchSlop();
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev)
    {
        boolean isCurrentItem = false;
        int position = pointToPosition((int) ev.getX(), (int) ev.getY());
        int currentSelectPosition = getSelectedItemPosition();
        if (position == currentSelectPosition)
        {
            isCurrentItem = true;
        }
        else
        {
            isCurrentItem = false;
        }

        if (ev.getAction() == MotionEvent.ACTION_DOWN)
        {
            e = MotionEvent.obtain(ev);
            if (isCurrentItem)
            {
                isIntercept = false;
            }
            else
            {
                isIntercept = true;

            }
        }
        else if (ev.getAction() == MotionEvent.ACTION_MOVE)
        {
            // 手指触摸的大小.........这儿我设的是20像素
            // if (Math.abs(ev.getX() - e.getX()) > scaledTouchSlop
            // || Math.abs(ev.getY() - e.getY()) > scaledTouchSlop)
            // {
            // bb = true;
            // }
        }
        else if (ev.getAction() == MotionEvent.ACTION_UP)
        {
            if (isCurrentItem)
            {
                isIntercept = false;
            }
            else
            {
                isIntercept = true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isIntercept = false;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        return isIntercept;
    }
}
